package com.situ.day21.io;

import org.junit.Test;

import java.io.*;

public class IODemo {

    @Test
    public void testFileReader() {
        Reader fileReader = null;
        try {
            fileReader = new FileReader("./src/io.txt");
            //public int read() 读取单个字符,如果已到达流的末尾，则返回 -1
            int ch1 = fileReader.read();
            System.out.println(ch1);
            System.out.println((char)ch1);

            int ch2 = fileReader.read();
            System.out.println(ch2);
            System.out.println((char)ch2);

            int ch3 = fileReader.read();
            System.out.println(ch3);
            System.out.println((char)ch3);

            int ch4 = fileReader.read();
            System.out.println(ch4);
            System.out.println((char)ch4);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testFileReader2() {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader("./src/io.txt");
            int ch = -1;
            //while不停去读，直到ch=-1，读到文件结尾，就退出循环
            while ((ch = fileReader.read()) != -1) {
                System.out.print((char)ch);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testFileReader3() {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader("./src/io.txt");
            //while不停去读，直到ch=-1，读到文件结尾，就退出循环
            //public int read(char[] cbuf)
            //将字符读入数组。在某个输入可用、发生 I/O 错误或者已到达流的末尾前，此方法一直阻塞。
            char[] buffer = new char[20];
            int length = -1;
            while ((length = fileReader.read(buffer)) != -1) {
                System.out.println(length);
                System.out.println(buffer);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testFileWriter() {
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        try {
            fileReader = new FileReader("./src/io.txt");
            fileWriter = new FileWriter("./src/io_back1.txt");
            char[] buffer = new char[20];
            int length = -1;
            while ((length = fileReader.read(buffer)) != -1) {
                System.out.println(length);
                System.out.println(buffer);
                // //读出多少就写多少，最后一次读取的数据可能不够buffer数组长度的数据
                //fileWriter.write(buffer);
                fileWriter.write(buffer, 0, length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testInputStream() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream("./src/aa.jpg");
            fileOutputStream = new FileOutputStream("./src/aa_back.jpg");
            byte[] buffer = new byte[1024];
            int length = -1;
            while ((length = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0 , length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testObjectOutputStream() {
        Student student = new Student(1, "zhansgan", 23);
        //把对象转换成IO流
        ObjectOutputStream objectOutputStream = null;
        //IO写文件
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("student");
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(student);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (objectOutputStream != null) {
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testObjectInputStream() {
        ObjectInputStream objectInputStream = null;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("student");
            objectInputStream = new ObjectInputStream(fileInputStream);
            Student student = (Student) objectInputStream.readObject();
            System.out.println(student);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (objectInputStream != null) {
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
